package com.me.db.domain;


import java.io.Serializable;
import java.util.Map;

import com.google.common.collect.Maps;

/**
 * rest通用结果对象 
 */
public class Result implements Serializable {

	private static final long serialVersionUID = 1L;
	/**
	 * 状态码
	 * @see com.i91ku.common.Result.Code
	 */
	private int code = Code.SUCCESS.value;

	/**
	 * 数据对象
	 */
	private Map<String, Object> resp;

	/**
	 * 状态信息
	 */
	private String msg;

	/**
	 * 封装结果对象
	 * @param code 状态码
	 * @param msg 状态信息
	 * @param resp 待封装结果对象
	 */
	private Result(Code code, String msg, Map<String, Object> resp) {
		this.code = code.value;
		this.resp = resp;
		this.msg = msg;
	}

	public static Result success(String key, Object value) {
		Map<String, Object> resp = Maps.newHashMap();
		resp.put(key, value);
		return new Result(Code.SUCCESS, null, resp);
	}

	public static Result success(String msg) {
		return new Result(Code.SUCCESS, msg, null);
	}

	public static Result success() {
		return new Result(Code.SUCCESS, Code.SUCCESS.msg, null);
	}

	public Result append(String key, Object value) {
		resp.put(key, value);
		return this;
	}

	@Deprecated
	public static Result error(String msg) {
		return new Result(Code.ERROR, msg, null);
	}

	public static Result error(Code code) {
		return new Result(code, code.msg, null);
	}

    public static Result error(Code code, String msg) {
        return new Result(code, msg, null);
    }

	public int getCode() {
		return code;
	}

	public Map<String, Object> getResp() {
		return resp;
	}

	public String getMsg() {
		return msg;
	}

	/**
	 * 拓展状态码
	 */
	public enum Code {
		ERROR(1, "delete"),

        // 正确的
		SUCCESS(20000, "成功"),

        // 用户和管理员请求的基本错误
		PERMISSION_DENIED(20001, "没有权限"), 
		// 登录超时
		LOGIN_TIMEOUT(20002, "登录超时"), 
		//登录失败 
		NO_USER(20003, "登录失败,用户名或者密码错误"), 
		// 用户没有登录
        NO_LOGIN(20004,"用户未登录"), 
        // 用户已被禁用
        USER_DISABLE(20005,"用户已被禁用"),
        
        NO_UPDATE(20006,"更新失败"),
        
        
        NO_CAMERA_NO(20007,"没有该摄像头序列号"),
        
        
        
        //支付系统错误
        INVENTORY_NOT_ENOUGH(20100,"Inventory is not enough use"),
        TOTAL_MONEY_PROBLEMS(20101,"total Money problems"),
        NO_HAVE_ENOUGH_MONEY(20102,"Users don't have enough money"),
        ORDER_ERROR(20103,"create Order error"),
        
        PARAMETER_ERROR(30001, "Parameter error"),
		REQUEST_TYPE_ERROR(30002, "Request type error"),

		IMAGE_SIZE_TOO_LARGE(40001, "Image size too large"),
		REPEAT_SUBMITTED(40002, "Repeat submitted"),


		ZIP_FILE_FORMAT_ERROR(40011, "zip file format name error"),
		ZIP_FILE_CONTENT_ERROR(40012, "zip file content error"),
		UNZIP_FILE_FAILED(40013, "unzip file failed"),
		IMAGE_NOT_EXIST(40110, "image id not exist"),
		VIDEO_NOT_EXIST(40109, "VIDEO id not exist"),
		IMAGE_NOT_SELECTED(40111, "image not selected"),
		IMAGE_TYPE_UNSUPPORTED(40112, "image type unsupported, only suport JPG, GIF, PNG"),
		IMAGE_INSERT_FAILED(40113, "image insert failed"),
		IMAGE_UPLOAD_EXCEPTION(40114, "image upload exception"),
		IMAGE_DELETE_FAILED(40115, "image delete failed"),
		IMAGE_UPDATE_FAILED(40116, "image update failed"),
		LABEL_NOT_EXIST(40130, "label not exist"),
		LABEL_NAME_REPEAT(40131, "label name repeat"),
		LABEL_INSERT_FAILED(40132, "label insert failed"),
		LABEL_UPDATE_FAILED(40133, "label update failed"),
		LABEL_DELETE_FAILED(40134, "label delete failed"),
		LABEL_MERGE_FAILED(40135, "label merge failed"),
		ATLAS_NOT_EXIST(40160, "atlas not exist"),
		ATLAS_INSERT_FAILED(40161, "atlas insert failed"),
		ATLAS_DELETE_FAILED(40162, "atlas delete failed"),
		ATLAS_UPDATE_FAILED(40163, "atlas update failed"),

		TIMER_EXECUTE(40180, "timer add failed"),
		TIMER_CANCEL(40181, "timer cancel failed"),

		COMMENT_DELETE_FAILED(40200, "comment delete failed"),
		CANCEL_ATTENTION_FAILED(40201, "cancel attention failed"),
		ALREADY_ATTENTION(40202, "already attention"),
		ALREADY_PRAISE(40203, "already praise"),
		CANCEL_PRAISE_FAILED(40204, "cancel praise failed"),
		ATTENTION_NOT_EXIST(40205, "entity not exist"),
		No_PRODUCTID(40206, "no productId"),


        // 用户系统错误
		NO_UNIONID(40288, "no unionid"),
		NO_SEX(40299, "no sex"),
		NO_PHONE(40300, "no phone"),
        NO_NICKNAME(40301, "no nickname"),
        NICKNAME_REPEAT(40302, "Nickname repeat"),
        USERNAME_OR_PASSWORD_ERROR(40303, "username or password error"),
        PASSWORD_ERROR(40304, "Password error"),
        NO_PASSWORD(40305,"No password"),
        NO_USERNAME(40306,"No username"),
        REGISTER_ERROR(40307, "register error"),
        UPDATE_USER_ERROR(40308, "update user error"),
        USERNAME_EXIST(40309, "username is exist"),
        NO_HEADER_FILE(40310, "no header file"),
        ADD_ADMIN_ERROR(40311, "add admin error"),
        DELETE_ADMIN_ERROR(40312, "delete admin error"),
        ADD_PERMISSION_ERROR(40313, "add permission error"),
        DELETE_PERMISSION_ERROR(40314, "delete permission error"),
        SEARCH_USER_ERROR(40315, "search user error"),
        UPDATE_USER_LEVEL_ERROR(40316, "update user level error"),
        NICKNAME_ILLEGAL_TYPE_ERROR(40317, "the nickname type is illegal"),
        LIST_USER_ERROR(40318, "list user error"),
        ADD_PERMISSION_URL_ERROR(40319, "add permission url error"),
        DELETE_PERMISSION_URL_ERROR(40320, "delete permission url error"),
        ADD_PERMISSION_ROLE_ERROR(40321, "add permission role error"),
        DELETE_PERMISSION_ROLE_ERROR(40322, "delete permission role error"),
        ADD_PERMISSION_ROLE_URL_ERROR(40323, "add permission role url error"),
        DELETE_PERMISSION_ROLE_URL_ERROR(40324, "delete permission role url error"),
        VALICODE_IS_cooling(40325, "valicode_is_cooling"),
        VALICODE_IS_ERROR(40326, "valicode_is_error"),
        PASSWORD_OR_PHONE_ERROR(40327, "Password or phone error"),
        LIVE_iS_OVER(40328, "LIVE IS OVER"),
        NO_SPACENUM(40329,"no spaceNum"),
        
        
        CAMERA_PASS_ERROR(40500,"CAMERA PASS ERROR"),
        NO_PRIMARY_KEY(40501,"NO_PRIMARY_KEY"),
        
		SYSTEM_ERROR(50001, "System error"),
		INVENTORY_ERROR(50002, "System error"),
		
		REMOTE_SERVICE_ERROR(50005, "Remote service error"),;

		
		

		
		
		/**
		 * 
		 */
		
		/**
		 * 状态码值
		 */
		private int value;
		private String msg;

		Code(int value, String msg) {
			this.value = value;
			this.msg = msg;
		}
		int getCode(Code code){
			return value;
		}

		public int getcode(){
			return this.value;
			
		}
		public int getCode() {
			// TODO Auto-generated method stub
			return this.value;
		}
		public String getMsg() {
			// TODO Auto-generated method stub
			return this.msg = msg;
		}
		
	}

}
